﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nox.Amuse.MVVM;
using Nox.Amuse.Model;
using System.Collections.ObjectModel;
using System.Windows.Input;
using System.Windows;
using Nox.Amuse.View;

namespace Nox.Amuse.ViewModel
{
    public class WorldExplorerViewModel : ObservableObject
    {
        private ObservableCollection<WorldDataViewModel> myWorlds = new ObservableCollection<WorldDataViewModel>();
        public ObservableCollection<WorldDataViewModel> Worlds
        {
            get { return myWorlds; }
        }

        private WorldDataViewModel mySelectedWorld;
        public WorldDataViewModel SelectedWorld
        {
            get { return mySelectedWorld; }
            set
            {
                mySelectedWorld = value;
                base.RaisePropertyChanged("SelectedWorld");
            }
        }

        private DataIndex myIndexModel = DataIndex.Load();

        public WorldExplorerViewModel()
        {
            RefreshWorldList();
        }

        private void RefreshWorldList()
        {
            Worlds.Clear();
            foreach (var worldName in myIndexModel.WorldNames)
            {
                Worlds.Add(new WorldDataViewModel(WorldData.Load(worldName)));
            }
        }

        public ICommand AddWorldCommand
        {
            get
            {
                return myAddWorldCommand ?? (myAddWorldCommand = new RelayCommand(
                    () =>
                    {
                        // Find the window using this class as its data context
                        var w = new WorldDataViewModel();
                        var addNew = new SimpleNewWorldDialog { DataContext = w, Owner = this.GetViewModelWindow() };
                        addNew.ShowDialog();
                        if (w.WorldSaved)
                        {
                            myIndexModel.WorldNames.Add(w.DisplayName);
                            myIndexModel.Save();
                            Worlds.Add(new WorldDataViewModel(WorldData.Load(w.DisplayName)));
                        }
                    }
                    ));
            }
        }
        private RelayCommand myAddWorldCommand;

        public ICommand DeleteWorldCommand
        {
            get
            {
                return myDeleteWorldCommand ?? (myDeleteWorldCommand = new RelayCommand(
                    () =>
                    {
                        // Open UI and all that stuff
                        if (MessageBox.Show(String.Format("Are you sure you want to delete {0}?", SelectedWorld.DisplayName),
                                "Placeholder alert",
                                MessageBoxButton.YesNo,
                                MessageBoxImage.None,
                                MessageBoxResult.No) == MessageBoxResult.Yes)
                        SelectedWorld.Delete();
                        myIndexModel.WorldNames.Remove(SelectedWorld.DisplayName);
                        myIndexModel.Save();
                        RefreshWorldList();
                    },
                    () =>
                    {
                        // Check if world is selected
                        return SelectedWorld != null;
                    }
                    ));
            }
        }
        private RelayCommand myDeleteWorldCommand;

        public ICommand ConnectToWorldCommand
        {
            get
            {
                return myConnectToWorldCommand ?? (myConnectToWorldCommand = new RelayCommand(
                    () =>
                    {
                        // Close UI and then perform Connection
                        App.Messenger.NotifyColleagues(Messages.CloseDialog, this);
                        App.Messenger.NotifyColleagues(Messages.TabAddRequest, new TabViewModel(WorldData.Load(SelectedWorld.DisplayName)));
                    },
                    () =>
                    {
                        // Check if world is selected
                        return SelectedWorld != null;
                    }
                    ));
            }
        }
        private RelayCommand myConnectToWorldCommand;

    }
}
